var fakedata=require("../../static/js/fakedata");
var app=getApp();
Page({
    data: {
        content:'',
        //下面是图片上传有关的变量
        filePath:'',
        picurl:'',
        userid:'',
        nickname:''
    },
    // 下面是图片上传的相关功能
    /**
     * 选择图片的功能
     */
    chooseImage() {
        swan.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: res => {
                this.setData('filePath', res.tempFilePaths[0]);
            }
        });
    },
    // 上传文件功能
    uploadFile() {
        const filePath = this.getData('filePath');
        if (filePath=='') {
            swan.showToast({
                title: '请先上传图片',
                icon: 'none'
            });
        }else{
            swan.uploadFile({
                url: fakedata.urls.upload,
                filePath,
                name: 'image',
                header: {
                    'content-type': 'multipart/form-data'
                },
                formData: {
                    file:this.data.filePath
                },
                success: res => {
                    swan.showToast({
                        title: '上传成功',
                        icon: 'none'
                    });
                    console.log('uploadFile success', res);
                    this.setData({
                        picurl:res
                    });
                },
                fail: err => {
                    console.log('uploadFile fail', err);
                    swan.showToast({
                        title: '上传失败',
                        icon: 'none'
                    });
                }
            });
        }
    },
    //拿到用户id
    onLoad(e){
        if(app.globalData.islogin){
            this.setData({
                userid:app.globalData.userinfo.id,
                nickname:app.globalData.userinfo.nickname,
            });
        }
    },
    onShow(){
        if(!app.globalData.islogin){
            swan.navigateTo({
                url:'/pages/login/login'
            });
            swan.showModal({
                title:'您还没有登录'
            });
        }
    },
    updateData(e){
        this.setData({
            content:e.detail.value
        });
    },
    publish(){
        if(this.data.picurl=='' || this.data.content==''){
            swan.showModal({
                title:'提示',
                content:'请您完整填写信息'
            });
            return;
        }
        let all_data={
            userid:this.data.userid,
            nickname:this.data.nickname,
            descpic:this.data.picurl,
            content:this.data.content
        };
        console.log(all_data);
        swan.request({
            url: fakedata.urls.publishforum,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            data: all_data,
            success: res => {
                console.log(res);
                swan.showModal({
                    title:'发布成功'
                });
                swan.switchTab({
                    url: '/pages/forum/forum'
                });
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    }
});